package com.wlient.basic.Stream;

import com.wlient.basic.pojo.book;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author: 史炜立
 * @description: TODO
 * @date: 2022/8/8 10:03
 */
public class bookMapTest {

    public static void main(String[] args) {
        List<book> list1 = new ArrayList<book>();
        List<book> list2 = new ArrayList<book>();

        for (int i = 0; i < 5; i++) {
            book b = new book();
            b.setName("A");
            b.setNumber(i);
            b.setPrice(i + 2);
            b.setVoteNumber(i + 3);
            list1.add(b);
        }

        for (int i = 2; i < 8; i++) {
            book b = new book();
            b.setName("A");
            b.setNumber(i);
            b.setPrice(i + 2);
            b.setVoteNumber(i + 3);
            list2.add(b);
        }

        Map<Integer, book> map1 = list1.stream().collect(Collectors.toMap(book::getNumber, o -> o));

        Map<Integer, book> map2 = list2.stream().collect(Collectors.toMap(book::getNumber, o -> o));

        Set<Integer> set = new HashSet<>();
        set.addAll(map1.keySet());
        set.addAll(map2.keySet());
        List<book> list = new ArrayList<>();
        set.stream().forEach(i -> {
            book b1 = map1.get(i);
            book b2 = map2.get(i);
            book b3 = new book();
            b3.setNumber(i);
            b3.setVoteNumber((b1 == null ? 0 : b1.getVoteNumber()) + (b2 == null ? 0 : b2.getVoteNumber()));
            list.add(b3);
        });

        System.out.println(list);
    }
}
